#include "apue.h"
#include <ctype.h>
#include <errno.h>

int main(void) {
    char buf[MAXLINE];
    int i, c;
    while((fgets(buf, MAXLINE, stdin)) != NULL) {
        for (i=0; i<strlen(buf); ++i) {
            c= buf[i];
            if (isupper(c))
                c= tolower(c);
            else if (islower(c))
                c= toupper(c);

            if (putc(c, stdout) == EOF)
                err_sys("output error");

            if (c == '\n')
                fflush(stdout);
        }
    }
    if (errno > 0)
        err_sys("fgets error");
    exit(0);
}
